import * as phaser from "phaser";
import {RatingSystem} from "../sys/rating.ts";
import {Boom} from "./Boom.ts";

/**
 * 目标
 */
export class Target extends phaser.Physics.Arcade.Sprite {

    index: number = -1;
    type: string = '';
    private declare ratingSystem: RatingSystem;
    private declare readonly orderly: boolean;

    constructor(scene: phaser.Scene, ratingSystem: RatingSystem, orderly: boolean, type: string, index: number) {
        const x = phaser.Math.Between(40, scene.cameras.main.width - 40);
        const texture = index >= 10 ? 'SpritesDisturb' : 'SpritesTarget';
        const idx = index >= 10 ? index - 10 : index;
        super(scene, x, 0, texture, `target${idx}`);
        this.ratingSystem = ratingSystem;
        this.orderly = orderly;
        scene.add.existing(this);
        scene.physics.add.existing(this);
        this.setScale(1.4, 1.4);
        this.index = index;
        this.type = type;
    }

    /**
     * 开始下落
     */
    start(velocity: number) {
        this.setVelocityY(velocity);
    }

    /**
     * 碰撞目标，但不是指定目标，反应失败
     */
    overlapTargetUnSpecified() {
        if (!this.active || !this.visible) return
        const texture = this.index >= 10 ? 'SpritesDisturbError' : 'SpritesTargetError';
        const index = this.index >= 10 ? this.index - 10 : this.index;
        new Boom(this.scene, this.x, this.y, texture, `target${index}`);
        this.exit();
        this.ratingSystem.reactionError();
    }

    /**
     * 碰撞
     */
    overlap() {
        if (!this.active || !this.visible) return
        if (this.type == 'target') { // 撞击目标：反应成功
            const texture = this.index >= 10 ? 'SpritesDisturbRight' : 'SpritesTargetRight';
            const index = this.index >= 10 ? this.index - 10 : this.index;
            new Boom(this.scene, this.x, this.y, texture, `target${index}`);
            this.exit();
            this.ratingSystem.reactionRight(RatingSystem.TARGET);
            return;
        }
        // 撞击干扰：抑制失败
        const texture = this.index >= 10 ? 'SpritesDisturbError' : 'SpritesTargetError';
        const index = this.index >= 10 ? this.index - 10 : this.index;
        new Boom(this.scene, this.x, this.y, texture, `target${index}`);
        this.ratingSystem.restrainError()
        this.exit();
    }

    preUpdate(time: number, delta: number) {
        super.preUpdate(time, delta);
        let {height} = this.scene.cameras.main;
        if (this.y >= height + 20) {
            this.exit();
            // 目标自行销毁，反应失败
            if (this.type == 'target') {
                if (this.orderly) { // 顺序模式，出现是目标，没撞，则抑制成功
                    this.ratingSystem.restrainRight()
                } else {
                    this.ratingSystem.reactionError(true)
                }
            } else {
                this.ratingSystem.restrainRight()
            }
        }
    }

    private exit() {
        this.disableBody(true, true);
        this.destroy(true);
        if (this.body) {
            this.scene.physics.world.remove(this.body);
        }
    }
}